﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HZP.CKGL
{
    public partial class MAIN : Form
    {
        DataGridViewCellStyle sJLD, sXSD, sCPD, sLSZD,sCJLWD;
        public MAIN()
        {
            InitializeComponent();
            getRowStyle();
            addButton(flowLeft, new ButtonConfig { Text = "查询记录", Click = Btn_CXJL, Tooltip = "查询阶段内,四种数据的所有发生的记录!" });
            addButton(flowLeft, new ButtonConfig { Text = "库存量记录", Click = Btn_PDKC, Tooltip = "显示生产+厂家购入-工地销售的差值!" });
        }
        //返回日期
        private string getRQRange()
        {
            string start = dtimeStart.Value.ToString("yyyy-MM-dd");
            string end = dtimeEnd.Value.ToString("yyyy-MM-dd");
            return "rq>='" + start + "' and rq<='" + end + "'";
        }
        //反射打开窗体
        private void openFrm(string frmName)
        {
            Type t = Type.GetType("HZP.CKGL." + frmName);
            (t.Assembly.CreateInstance(t.FullName) as Form).ShowDialog();
        }
        private void toolboxOpenfrmbyTag(object sender, EventArgs e)
        {
            openFrm((sender as ToolStripButton).Tag.ToString());
        }
        //创建默认颜色
        private void getRowStyle()
        {
            this.sJLD = new DataGridViewCellStyle();
            this.sJLD.BackColor = Color.LightBlue;
            this.sJLD.SelectionBackColor = Color.LightSteelBlue;
            cbJld.BackColor = Color.LightBlue;

            this.sCPD = new DataGridViewCellStyle();
            this.sCPD.BackColor = Color.LightGray;
            this.sCPD.SelectionBackColor = Color.LightSlateGray;
            cbCPD.BackColor = Color.LightGray;

            this.sXSD = new DataGridViewCellStyle();
            this.sXSD.BackColor = Color.LightPink;
            this.sXSD.SelectionBackColor = Color.HotPink;
            cbXSD.BackColor = Color.LightPink;

            this.sLSZD = new DataGridViewCellStyle();
            this.sLSZD.BackColor = Color.LightSkyBlue;
            this.sLSZD.SelectionBackColor = Color.DeepSkyBlue;
            cbLSZD.BackColor = Color.LightSkyBlue;

            this.sCJLWD = new DataGridViewCellStyle();
            this.sCJLWD.BackColor = Color.LightSalmon;
            this.sCJLWD.SelectionBackColor = Color.DarkSalmon;
            cbCJLWD.BackColor = Color.LightSalmon;
        }
        //统一给不同类型设置上颜色
        private void dgvList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 0)
            {
                DataGridViewRow CurrentRow = dgvList.Rows[e.RowIndex];
                CurrentRow.HeaderCell.Value = Convert.ToString(e.RowIndex + 1);

                DataRowView dr = (dgvList.DataSource as DataView)[e.RowIndex];
                if (dr["TN"] != null)
                {
                    if (dr["TN"].ToString() == "JLD")
                    {
                        CurrentRow.DefaultCellStyle = sJLD;
                    }
                    if (dr["TN"].ToString() == "GDXSD")
                    {
                        CurrentRow.DefaultCellStyle = sXSD;
                    }
                    if (dr["TN"].ToString() == "CPD")
                    {
                        CurrentRow.DefaultCellStyle = sCPD;
                    }
                    if (dr["TN"].ToString() == "LSZD")
                    {
                        CurrentRow.DefaultCellStyle = sLSZD;
                    }
                    if (dr["TN"].ToString() == "CJLWD")
                    {
                        CurrentRow.DefaultCellStyle = sCJLWD;
                    }
                }
            }
        }
        //统一给出提示
        private void dgvList_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
            DataRowView dr = (dgvList.DataSource as DataView)[e.RowIndex];
            if (dr["TN"] != null)
            {
                if (dr["TN"].ToString() == "JLD")
                {
                    e.ToolTipText = "进料单";
                }
                if (dr["TN"].ToString() == "GDXSD")
                {
                    e.ToolTipText = "工地来往单";
                }
                if (dr["TN"].ToString() == "CJLWD")
                {
                    e.ToolTipText = "厂家来往单";
                }
                if (dr["TN"].ToString() == "CPD")
                {
                    e.ToolTipText = "产品单";
                }
                if (dr["TN"].ToString() == "LSZD")
                {
                    e.ToolTipText = "流水帐单";
                }
            }
        }
        //给控件添加系统按钮
        private void addButton(Control parent, ButtonConfig config)
        {
            Button btn = new Button();
            btn.Text = config.Text;
            tipMain.SetToolTip(btn, config.Tooltip);
            if (config.Click != null)
            {
                btn.Click += config.Click;
            }
            parent.Controls.Add(btn);
        }
        //日期右键菜单
        private void mrqToday_Click(object sender, EventArgs e)
        {
            var menu = sender as ToolStripMenuItem;
            var today = DateTime.Today;
            switch (menu.Text)
            {
                case "当天":
                    break;
                case "本月1号":
                    today = new DateTime(today.Year, today.Month, 1);
                    break;
                case "向前1个月":
                    today = dtimeStart.Value;
                    today = today.AddMonths(-1);
                    break;
                case "当年1号":
                    today = new DateTime(today.Year, 1, 1);
                    break;
                case "最早日期":
                    today = new DateTime(2015, 1, 1);
                    break;
                default:
                    break;
            }
            dtimeStart.Value = today;

        }
        //查询当时产生情况
        private void Btn_CXJL(object sender, EventArgs e)
        {
            string rq = getRQRange();
            List<string> list = new List<string>();
            string sql = "";
            //绑定详情
            UTIL.InitDGVColumns(dgvList, new ColConfig[]
           {
                new ColConfig {ColName="日期",FieldName="RQ" },
                new ColConfig {ColName="名称",FieldName="JLMC",Width=150},
                new ColConfig {ColName="数量",FieldName="SL",Width=60},

                new ColConfig {ColName="总价",FieldName="ZJ",Width=60 },
                new ColConfig {ColName="已付款",FieldName="YFK",Width=60 },
                new ColConfig {ColName="未付款",FieldName="WFK",Width=60 },
                new ColConfig {ColName="备注",FieldName="BZ",Width=200 }
           });
            if (cbJld.Checked) list.Add(  $"select TN,RQ,                  JLMC, SL,      ZJ,YFK,      WFK,BZ   from VW_JLD where {rq}");
            if (cbCPD.Checked) list.Add(  $"select TN,RQ,CPMC+'('+CPXH+')' JLMC, SL,JGF   ZJ,'' YFK,'' WFK,BZ  from VW_CPD where {rq}");
            if (cbCJLWD.Checked) list.Add($"SELECT TN,RQ,CPMC+'('+CPXH+')' JLMC, SL,      ZJ,YFK,       WFK,BZ FROM VW_CJLWD where {rq}");
            if (cbXSD.Checked) list.Add(  $"SELECT TN,RQ,CPMC+'('+CPXH+')' JLMC, SL,      ZJ,YFK,       WFK,BZ FROM VW_GDXSD where {rq}");
            if (cbLSZD.Checked) list.Add($"SELECT TN,RQ,LSZMC as           JLMC, 0 SL,    ZJ,YFK,       WFK,BZ FROM VW_LSZD where {rq}");
            if (list.Count == 0) MessageBox.Show("必须要选择一个表,才能对其查询");

            sql = string.Join(" union all ", list.ToArray());

            DataTable dt = DB.CKGL.Query(sql);
            dgvList.DataSource = dt.DefaultView;

            //绑定汇总
            UTIL.InitDGVColumns(dgvSumery, new ColConfig[]
           {
                new ColConfig {ColName="名称",FieldName="JLMC",Width=150},
                new ColConfig {ColName="数量",FieldName="SL",Width=60},

                new ColConfig {ColName="总价",FieldName="ZJ",Width=60 },
                new ColConfig {ColName="已付款",FieldName="YFK",Width=60 },
                new ColConfig {ColName="未付款",FieldName="WFK",Width=60 }
           });
            sql = $"select JLMC,SUM(SL) SL,SUM(ZJ) ZJ,SUM(YFK) YFK,SUM(WFK) WFK  from ({sql}) as a  group by JLMC with cube";
            dgvSumery.DataSource = DB.CKGL.Query(sql);
        }


        //查询库存数量
        private void Btn_PDKC(object sender, EventArgs e)
        {
            UTIL.InitDGVColumns(dgvList, new ColConfig[]
           {
                new ColConfig {ColName="日期",FieldName="RQ" },
                new ColConfig {ColName="名称",FieldName="JLMC",Width=150},
                new ColConfig {ColName="数量",FieldName="SL",Width=60},

                new ColConfig {ColName="总价",FieldName="ZJ",Width=60 },
                new ColConfig {ColName="已付款",FieldName="YFK",Width=60 },
                new ColConfig {ColName="未付款",FieldName="WFK",Width=60 },
                new ColConfig {ColName="备注",FieldName="BZ",Width=200 }
           });
            string rq = getRQRange();
            string sql = $@"
 select TN,RQ, CPMC+'('+CPXH+')' JLMC,SL, JGF ZJ,'' YFK,'' WFK,BZ  from VW_CPD where {rq}
 UNION all
 SELECT TN,RQ, CPMC+'('+CPXH+')' JLMC,SL,     ZJ,   YFK,   WFK,BZ  FROM vw_cjlwd where {rq}
 UNION all
 SELECT TN,RQ, CPMC+'('+CPXH+')' JLMC,SL,     ZJ,   YFK,   WFK,BZ  FROM vw_GDXSD where {rq}
";
            DataTable dt = DB.CKGL.Query(sql);
            dgvList.DataSource = dt.DefaultView;
            //绑定汇总
            UTIL.InitDGVColumns(dgvSumery, new ColConfig[]
           {
                new ColConfig {ColName="名称",FieldName="JLMC",Width=150},
                new ColConfig {ColName="数量",FieldName="SL",Width=60},

                new ColConfig {ColName="总价",FieldName="ZJ",Width=60 },
                new ColConfig {ColName="已付款",FieldName="YFK",Width=60 },
                new ColConfig {ColName="未付款",FieldName="WFK",Width=60 }
           });
            sql = $"select JLMC,SUM(SL) SL,SUM(ZJ) ZJ,SUM(YFK) YFK,SUM(WFK) WFK  from ({sql}) as a  group by JLMC with cube";
            dgvSumery.DataSource = DB.CKGL.Query(sql);
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            throw new Exception("this is the demo");
        }


        private void dgvList_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {



        }
    }
}
